<?php

namespace App\Http\Controllers\Site\Custom;

use App\B2BProject\Site\HelperControllerBuildTrait;
use Illuminate\Http\Request;
use App\Http\Requests;
use App\Http\Controllers\Controller;

class MessageController extends Controller
{
    use HelperControllerBuildTrait;

    public function getIndex(Request $request)
    {
        $request->query->set('page', $request->query('page', $request->page));
        $pageSize = $request->query('pageSize', $request->pageSize);

        if ($request->ajax()) {
            $messages = \Auth::user()->messages()->paginate($pageSize);

            $data = [];

            foreach ($messages as $message) {
                $data[] = [
                    'id'      => $message->id,
                    'uname'   => '系统信息',
                    'content' => $message->content,
                    'time'    => date('Y-m-d H:i:s', strtotime($message->created_at)),
                ];
            }

            return response()->json([
                'data'           => $data,
                'pagingConfData' => [
                    'page'     => $messages->currentPage(),
                    'pageSize' => $pageSize,
                    'total'    => $messages->total(),
                ],
            ]);
        }

        // TODO [2015-11-09] 时间不足，功能待完善
        return view('host/custom/message/index');
    }

    public function getUnread(Request $request)
    {
        $request->query->set('page', $request->query('page', $request->page));
        $pageSize = $request->query('pageSize', $request->pageSize);

        if ($request->ajax()) {

        }
    }
}
